import axios from '../AxiosConfig.js'

export const createExchange = (data) => {
    return axios({
        method: 'put',
        url: '/api/exchanges/' + encodeURIComponent(data.vhost) + '/' + data.name,
        data: data
    })
}

export const deleteExchange = (vhost, name) => {
    return axios({
        method: 'delete',
        url: '/api/exchanges/' + encodeURIComponent(vhost) + '/' + name,
        params: {
            "if-unused": true
        }
    })
}

export const selectExchange = (vhost, name, msgRates) => {
    let split = msgRates.split("_");
    return axios({
        method: 'get',
        url: '/api/exchanges/' + encodeURIComponent(vhost) + '/' + name,
        params: {
            "msg_rates_age": split[0],
            "msg_rates_incr": split[1]
        }
    })
}

export const listExchange = (name) => {
    return axios({
        method: 'get',
        url: '/api/exchanges',
        params: {
            "page": 1,
            "page_size": 500,
            "name": name,
            "use_regex": false,
            "pagination": true
        }
    })
}

export const listExchangeByVhost = (vhost) => {
    return axios({
        method: 'get',
        url: "/api/exchanges/" + encodeURIComponent(vhost)
    })
}

export const listExchangeBinding = (vhost, name) => {
    return axios({
        method: 'get',
        url: '/api/exchanges/' + encodeURIComponent(vhost) + '/' + name + '/bindings/source',
    })
}

export const publishMessage = (data) => {
    if (data.name === undefined || data.name === ''){
        data.name = 'amq.direct'
    }
    return axios({
        method: 'post',
        url: '/api/exchanges/' + encodeURIComponent(data.vhost) + '/' + data.name + '/publish',
        data: {
            "properties": {},
            "routing_key": data.routing_key,
            "payload": data.payload,
            "payload_encoding": data.payload_encoding
        }
    })
}

